﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Configuration;
using OAProject.Domain.Interfaces;
using OAProject.Domain.Entities;
using OAProject.Infrastructure.Data;


namespace OAProject.Infrastructure.Data
{
     public class RoleUsersRepository: DapperRepository<Roles>, IRoleUsersrepository
    {
        //依赖注入
        public IConfiguration _configuration;

        public RoleUsersRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            //连接字符串
            this.ConnStr = _configuration.GetConnectionString("ConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }

        /// <summary>
        /// 获取父级的菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Menus> GetParentMenu(int id)
        {
            string sql = $"select * from menus where MenuID IN (SELECT MenuID from rolemenus where RoleID = (SELECT RoleID FROM RoleUsers where UserId ={id})and ParentID=0)";
            return Conn.Query<Menus>(sql).ToList();
        }
        /// <summary>
        /// 获取子集的菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Menus> GetChildMenu(int id)
        {
            string sql = $"select * from menus where ParentID={id}";
            return Conn.Query<Menus>(sql).ToList();

        }
        
        
    }
}
